﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Blog.Types.Repository;
using System.ComponentModel;
using System.Collections;

namespace Blog.Core
{
    public class Theme : BaseObject
    {
        private string _ThemeHeader;
        private string _Description;
        private MessageCollection _messages;
        
        public Theme()
        { 
//test
        }

        internal Theme(ITheme obj)
        {
            CopyFrom(obj);
        }

        private void CopyFrom(ITheme obj)
        { 
            _ID = obj.ID;
            _ThemeHeader = obj.ThemeHeader;
            _Description = obj.ThemeDescription;
        }

        private void CopyTo(ITheme obj)
        {
            obj.ID = _ID;
            obj.ThemeHeader = _ThemeHeader;
            obj.ThemeDescription = _Description;
        }

        public string ThemeHeader
        {
            get
            {
                return _ThemeHeader;
            }
            set
            {
                if (_ThemeHeader == value)
                    return;

                _ThemeHeader = value;
                OnPropertyChanged();
            }
        }

        public object GetHtml()
        {
            return _Description;
        }

        public string Description
        {
            get
            {
                return _Description;
            }
            set
            {
                if (_Description == value)
                    return;

                _Description = value;
                OnPropertyChanged();
            }
        }

        protected override void DeleteExecute()
        {
            Factory.Instance.Repository.DeleteTheme(ID);
        }

        protected override void SaveExecute()
        {
            _ID = Factory.Instance.Repository.Save(ID, delegate(ITheme theme) { CopyTo(theme); });
        }

        public override string ToString()
        {
            return ThemeHeader;
        }

        public MessageCollection GetMessages(bool autoLoadOrReload = true)
        {
            if (_messages == null)
            {
                _messages = new MessageCollection(this);
                if (autoLoadOrReload)
                    _messages.Load();
            }
            else if (autoLoadOrReload)
                _messages.Load(true);
            
            return _messages;
        }
    }
}
